import { login, logout, getInfo, queryUserMenu as getUserMenu } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    userInfo: {},
    basicInfoFrom: {
      contractId: '',
      contractName: '',
      financialCode: '',
      companyName: '',
      iscontractRevoke: '',
      storeId: '',
      storeName: '',
      lessorName: '',
      assetsLocation: '',
      floorage: 0,
      area: 0,
      assetsType: '',
      leasePurpose: '',
      contractStartTime: '',
      contractEndTime: '',
      freeRentTerm: 0,
      freeRentTimeStart: '',
      freeRentTimeEnd: '',
      contractMoneyNum: '',
      isVatinvoice: 2,
      taxRate: 0,
      taxAmount: 0,
      // 定金对象
      cdcDeposit: {
        isHandsel: 2,
        handselMoney: 0,
        depositPaymentMode: '',
        followPaymentTime: '',
        rentPayment: 1,
        isDeposit: 1
      }
    }
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_USERINFO: (state, userInfo) => {
    state.userInfo = userInfo
  },
  SET_DATA: (state, data) => {
    state.basicInfoFrom = data
  }
}

const actions = {
  // 用户登录
  login({ commit }, userInfo) {
    const { username, password, code } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password, code: code }).then(response => {
        const { data, msg } = response
        commit('SET_TOKEN', data)
        setToken(data)
        resolve(msg)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取用户信息
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        const { data } = response

        if (!data) {
          return reject('获取登录信息失败，请重新登录~')
        }
        const { nickname, avatar } = data
        commit('SET_USERINFO', data)
        commit('SET_NAME', nickname)
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  queryUserMenu() {
    return new Promise((resolve, reject) => {
      getUserMenu().then(response => {
        const { data } = response

        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 用户退出
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout().then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 清除token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
